﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddAuthority.aspx.cs" Inherits="ValuationClient.SystemManage.WebForm1" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <link href="../jquery-easyui-1.5.2/themes/default/easyui.css" rel="stylesheet" />
    <link href="../jquery-easyui-1.5.2/themes/icon.css" rel="stylesheet" />
    <link href="../jquery-easyui-1.5.2/demo/demo.css" rel="stylesheet" />
    <script src="jquery-easyui-1.5.2/jquery.min.js"></script>
    <script src="../js/jquery-1.8.2.min.js"></script>
    <script src="../jquery-easyui-1.5.2/jquery.easyui.min.js"></script>
    <%--    <script src="../jquery-easyui-1.5.2/easyloader.js"></script>--%>
    <%--    <script src="../js/jquery.params.js"></script>--%>
</head>
<body>
    <div>
        <table title="分配权限" id="dg" class="easyui-datagrid" toolbar="#tb" rownumbers="true" pagination="true" style="height:175px;">
            <thead>
                <tr>
                    <th data-options="field:'itemid',width:80"></th>
                    <th data-options="field:'item',width:1400"></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <input type="checkbox" id="check" onclick="AllChecked()" />全选</td>

                </tr>
                <tr>
                    <td>角色：</td>
                    <td>
                        <span id="select1"></span>

                    </td>
                </tr>
                <tr>
                    <td>选择权限：</td>
                    <td>
                        <span id="authority"></span>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><a href="#" class="easyui-linkbutton" onclick="FenPei()">分配权限</a></td>
                </tr>
            </tbody>
        </table>
    </div>

</body>
</html>
<script>
    $(function () {
        GetSysRoles();
        GetSysAuthority()
    })
    $("#dg").datagrid({
        showHeader: false
    })
    //显示角色信息
    function GetSysRoles() {
        $.ajax({
            type: "get",
            url: "http://localhost:55439/api/Values/GetSysRoles/?name=" + name,
            async: false,
            success: function (obj) {

                obj = eval(obj);
                var html = "";
                html += '<select id="selected1" class="easyui-combobox" style="width:300px;">'
                for (var i = 0; i < obj.length; i++) {
                    html += "<option value=\"" + obj[i].roles_Id + "\">" + obj[i].roles_Name + "</option>"
                }
                html += " </select>"
                $("#select1").html(html);
            }
        })
    }
    //显示权限
    function GetSysAuthority() {
        $.ajax({
            type: "get",
            url: "http://localhost:55439/api/Values/GetSysAuthority?name=" + "",
            async: false,
            success: function (obj) {
                obj = eval(obj);
                var html = "";
                for (var i = 0; i < obj.length; i++) {

                    if (i == 0) {
                        html += "<input type=\"checkbox\" name=\"checkid\" class=\"mm\" value=\"" + obj[i].authority_Id + "\" />" + obj[i].authority_Name + ""
                    }
                    else if (i % 7 == 0) {
                        html += "<input type=\"checkbox\"  name=\"checkid\" class=\"mm\" value=\"" + obj[i].authority_Id + "\" />" + obj[i].authority_Name + "<br>"
                    }
                    else {
                        html += "<input type=\"checkbox\"  name=\"checkid\" class=\"mm\" value=\"" + obj[i].authority_Id + "\" />" + obj[i].authority_Name + ""
                    }
                }
                $("#authority").html(html);
            }
        })
    }
    //全选
    function AllChecked() {
        var ck = document.getElementById("check").checked;

        var k = document.getElementsByName("checkid");
        for (var c in k) {
            k[c].checked = ck;

        }
    }
    //分配权限
    function FenPei() {
        var id = "";
        for (var i = 0; i < $(".mm").length; i++) {
            if ($(".mm").eq(i).attr("checked") == "checked") {
                id += $(".mm").eq(i).val() + ",";
            }
        }
        if (id.length == 0) {
            $.messager.alert('分配权限', '至少选择一个', 'error')
            return;
        }
        else {
            var selected = $("#selected1").val();//获取下拉列表的值
            $.ajax({
                type: "post",
                url: "http://localhost:55439/api/Values/AddAuthorityRole/?id=" + id + "&RoleId=" + selected,
                success: function (r) {
                    if (r) {
                        $.messager.alert('分配权限', '分配成功', 'info')
                        location.href = "AddAuthority.aspx";
                        return;
                    }
                    $.messager.alert('分配权限', '分配失败', 'error')
                }
            })
        }
    }
</script>
